/**
 * 
 */
package cn.crm.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.springframework.stereotype.Service;

import cn.crm.bean.Right;
import cn.crm.dao.RightDao;
import cn.crm.service.RightService;

/**
 * @author Vi1___000
 *
 */
@Service("rightService")
public class RightServiceImpl implements RightService {

	@Resource
	private RightDao rightDao;
	
	/* (non-Javadoc)
	 * @see cn.crm.service.RightService#getByParentID(java.lang.String)
	 */
	@Override
	public String getByParentID(String rid) {
		// TODO Auto-generated method stub
		JSONArray jlist = new JSONArray();
		List<Right> list = rightDao.getByParentID(rid);
		for (Right right : list) {
			JSONObject obj = new JSONObject();
			obj.put("id", right.getRid());
			obj.put("name", right.getRname());
			obj.put("parentid",right.getParentid());
			obj.put("index", right.getZindex());
			obj.put("url", right.getRurl());
			jlist.add(obj);
		}
		return jlist.toJSONString();
	}

	/* (non-Javadoc)
	 * @see cn.crm.service.RightService#getByID(java.lang.String)
	 */
	@Override
	public String getByID(String id) {
		// TODO Auto-generated method stub
		Right right = rightDao.getByID(id);
		JSONObject obj = new JSONObject();
		if(right!=null){
			obj.put("id", right.getRid());
			obj.put("name", right.getRname());
			obj.put("parentid",right.getParentid());
			obj.put("index", right.getZindex());
			obj.put("url", right.getRurl());
			obj.put("noMenu", right.getZtype());
		}
		return obj.toJSONString();
	}

	/* (non-Javadoc)
	 * @see cn.crm.service.RightService#addRight(cn.crm.bean.Right)
	 */
	@Override
	public boolean addRight(Right right) {
		// TODO Auto-generated method stub
		return rightDao.addRight(right);
	}

	/* (non-Javadoc)
	 * @see cn.crm.service.RightService#updateRight(cn.crm.bean.Right)
	 */
	@Override
	public boolean updateRight(Right right) {
		// TODO Auto-generated method stub
		return rightDao.updateRight(right);
	}

	/* (non-Javadoc)
	 * @see cn.crm.service.RightService#delRight(cn.crm.bean.Right)
	 */
	@Override
	public boolean delRight(Right right) {
		// TODO Auto-generated method stub
		Right temp = rightDao.getByID(right.getRid());
		if(temp!=null){
			return rightDao.delRight(temp);
		}else{
			return false;
		}
	}

	public RightDao getRightDao() {
		return rightDao;
	}

	public void setRightDao(RightDao rightDao) {
		this.rightDao = rightDao;
	}

	@Override
	public Right findByID(String id) {
		// TODO Auto-generated method stub
		return rightDao.getByID(id);
	}
	
	

}
